/*!
 * @file object.cpp
 * @brief model.hh
 */

#include "engine/basic/object.hh"
using namespace std;

/*--------------------------------------------------------------------------*/

object::object(model *model3d, float width, point3f pos, point3f rot) :
	model3d(model3d), scale(), totalMass(), isPhysicEnabled(true),
			position(pos), rotation(rot) {

	const_cast<float&> (this->scale) = this->model3d->getSize().x / width;

	const list<modelMass*> *massList = this->model3d->getMassList();
	float totalMass = 0.0f;
	for (list<modelMass*>::const_iterator i = massList->begin(); i
			!= massList->end(); ++i) {
		totalMass += (*i)->weigth;
	}
	const_cast<float&> (this->totalMass) = totalMass;

	//this->v.x = 30.0f;
	this->rotation = point3f(0.0, 0.0, 0.0);
	point3f a(1.0, 0.0, 0.0);

	this->rotateVec(a);

	//pawirr::sys_quit(QUIT_BRUTAL);
	//rotation.x *= -1;
}

/*--------------------------------------------------------------------------*/

void object::draw() {

	glPushMatrix();

	glScalef(1.0f / this->scale, 1.0f / this->scale, 1.0f / this->scale);

	glTranslatef(this->position.x * this->scale,
			this->position.y * this->scale, this->position.z * this->scale);

	glRotatef(this->rotation.z * 180.0f / M_PI, 0.0f, 0.0f, 1.0f);
	glRotatef(this->rotation.y * 180.0f / M_PI, 0.0f, 1.0f, 0.0f);
	glRotatef(this->rotation.x * 180.0f / M_PI, 1.0f, 0.0f, 0.0f);

	this->model3d->draw(false);

	glPopMatrix();
}

/*--------------------------------------------------------------------------*/

void object::physic(float timeInMs) {
	//cout << this->rotation;
	//cout << this->position << this->v << this->a << "###" << endl;

	float persek = (timeInMs / 1000.0f);

	//this->a.y = pawirr::graphic->_env->gravityAcc;
	//this->v = this->v + this->a * persek;

	const list<modelMass*> *massList = this->model3d->getMassList();

	point3f forces;
	point3f newPosition;
	bool canMove = true;

	glBegin(GL_LINES);
	for (list<modelMass*>::const_iterator i = massList->begin(); i
			!= massList->end(); ++i) {

		newPosition = this->position + this->rotateVec((*i)->position
				/ this->scale) + this->v * persek;

		//glVertex3f(this->position.x, this->position.y, this->position.z);
		//glVertex3f(newPosition.x, newPosition.y, newPosition.z);

		if (!pawirr::m->world->_area->checkPos(newPosition)) {
			canMove = false;
			//this->v = point3f(0.0f, 0.0f, 0.0f);
			//break;
		}
	}
	glEnd();

	if (canMove) {
		//this->position = this->position + v * persek;
	}
}

/*--------------------------------------------------------------------------*/

point3f object::rotateVec(point3f a) {

	point3f b;
	point3f rotation = this->rotation;

	//rotation.x = this->rotation.x;
	//rotation.y = this->rotation.y;
	//rotation.z = M_PI - this->rotation.z;


	float a1 = cos(rotation.y) * cos(rotation.z);
	float a2 = cos(rotation.y) * sin(rotation.z);
	float a3 = -sin(rotation.y);

	float b1 = -cos(rotation.x) * sin(rotation.z) + sin(rotation.x) * sin(
			rotation.y) * cos(rotation.z);
	float b2 = cos(rotation.x) * cos(rotation.z) + sin(rotation.x) * sin(
			rotation.y) * sin(rotation.z);
	float b3 = sin(rotation.x) * cos(rotation.y);

	float c1 = sin(rotation.x) * sin(rotation.z) + cos(rotation.x) * sin(
			rotation.y) * cos(rotation.z);
	float c2 = -sin(rotation.x) * cos(rotation.z) + cos(rotation.x) * sin(
			rotation.x) * sin(rotation.z);
	float c3 = cos(rotation.x) * cos(rotation.y);

	b.x = a1 * a.x + b1 * a.y + c1 * a.z;
	b.y = a2 * a.x + b2 * a.y + c2 * a.z;
	b.z = a3 * a.x + b3 * a.y + c3 * a.z;

	return b;
}

/*--------------------------------------------------------------------------*/
